/*
 *  JDiv - API for Multimedia Application
 *  http://jdiv.sourceforge.net 
 *  ------------------------------------
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

package com.jdiv.internalClasses;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.jdiv.Const;
import com.jdiv.JDiv;
import com.jdiv.kernel.JDivResources;

public class JDivImage {

	public String stringAtlas;
	public int x, y, width, height;
	
	TextureRegion textureRegion;
	
	int fileId;     // FILE donde se encuentra la textura, compatibilidad con Bennu
	int idTexture;  // Identificador de la textura a mostrar
	
	//-----------------------------------------------------------------------------
	// JDivImage: Inicializa una imagen
	//-----------------------------------------------------------------------------	
	
	public JDivImage( int fileId, int idTexture, int x, int y, int width, int height ){
		 
		 this.idTexture = idTexture;
		 this.fileId = fileId;
		 this.x      = x;
		 this.y      = y;
		 this.width  = width;
		 this.height = height;
		 stringAtlas = "";
	}
	
	//-----------------------------------------------------------------------------
	// JDivImage: Inicializa una imagen Atlas
	//-----------------------------------------------------------------------------	
		
	public JDivImage( int idAtlas, String strAtlas ){
		 idTexture = idAtlas;
		 this.stringAtlas  = strAtlas;
		 x = y = width = height = 0;
	}
	 
	//-----------------------------------------------------------------------------
	// getImage: Obtenemos una imagen de una textura
	//-----------------------------------------------------------------------------	
		
	public Image getImage(int mirror)
	{
		    if( textureRegion == null ){
		    	
			 JDivTexture texture = JDivResources.listTextures.getJDivTexture(fileId, idTexture);
		
			 if( texture == null ){
				JDiv.system.log("ERROR, getImage() - Can not load image from texture id: " + idTexture);
				return null;
			 }
			
			 textureRegion = texture.getTextureRegion(this);
		    }	
			
			if( textureRegion != null ){
				
				if(this.width == 0){
					this.x      = textureRegion.getRegionX();
					this.y      = textureRegion.getRegionY();
					this.width  = textureRegion.getRegionWidth();
					this.height = textureRegion.getRegionHeight();
				 }
				
				 if( mirror == Const.MIRROR_NONE )
						 return new Image(textureRegion);
				 else
				 { 
						 Sprite sprite = new Sprite(textureRegion);
						  
						 // Espejado Horizontal y vertical
						 boolean hflip = ((mirror & Const.MIRROR_HORIZONTAL) == Const.MIRROR_HORIZONTAL);
						 boolean vflip = ((mirror & Const.MIRROR_VERTICAL  ) == Const.MIRROR_VERTICAL);
						  
						 sprite.flip(hflip, vflip);
									
						 return new Image(sprite);
				 }
			}			
				
			return null;
	}
	
	public TextureRegion getTextureRegion(){
		return textureRegion;
	}
	
	//-----------------------------------------------------------------------------
	// getImage: Obtenemos los datos de la imagen
	//-----------------------------------------------------------------------------	
			
	public void getData( )
	{
			JDivTexture texture = JDivResources.listTextures.getJDivTexture(fileId, idTexture);
			
			if( texture != null ){
			 texture.getImageData(this);
			}else
			 JDiv.system.log("ERROR, getData() - Can not get data from texture id: " + idTexture);
	}
	
	//-----------------------------------------------------------------------------
	// getImage: Obtenemos la textura de una imagen
	//-----------------------------------------------------------------------------		
	
	public JDivTexture getTexture(){
		    return JDivResources.listTextures.getJDivTexture(fileId, idTexture);
	}
}
